from lxml import etree

if __name__ == "__main__":
    #实例化一个etree对象，且将被解析的源码加载到该对象中
    tree = etree.parse('test.html')
    #r = tree.xpath('/html/body/div')
    #r = tree.xpath('/html//div')
    #r = tree.xpath('//div')
    #r = tree.xpath('//div[@class="song"]')
    #r = tree.xpath('//div[@class="song"]/p[3]')
    #r = tree.xpath('//div[@class="tang"]/ul/li[5]/a/text()')[0]
    #r = tree.xpath('//li[7]//text()')[0]
    #r = tree.xpath('//div[@class="tang"]//text()')
    #取属性
    r = tree.xpath('//div[@class="song"]/img/@src')
    print(r)
    # UA伪装：将对应的User-Agent封装到一个字典
    #headers = {
    #    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36'
    #}